package entities;

import entities.Group;

import java.io.Serializable;

import javax.persistence.*;

import org.apache.commons.codec.digest.DigestUtils;

import ejb.dto.UserDTO;

import java.util.Date;
import java.util.List;


@Entity
@Table(name="USERS")
@NamedQueries({
	@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
})
public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	private String userId;
	
	private String password;

	@ElementCollection(targetClass = Group.class)
    @CollectionTable(name = "USERS_GROUPS",
                    joinColumns = @JoinColumn(name = "USERID"))
    @Enumerated(EnumType.STRING)
    @Column(name="GROUPID")
    private List<Group> groups;

	private String lastName;

	private String firstName;

	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date registeredOn;

	
	//CONSTRUCTORS
	
	public User() {
	}

	public User(UserDTO user) {
		this.userId        = user.getEmail();
        this.firstName    = user.getFirstName();
        this.setLastName(user.getLastName()); 
		this.password     = DigestUtils.sha256Hex(user.getPassword() );
        this.registeredOn = new Date();
	}

	//GETTER AND SETTER
	
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Group> getGroups() {
		return this.groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}